package com.zut.dao.impl;

import com.alibaba.druid.util.StringUtils;
import com.zut.dao.AdminShopDao;
import com.zut.pojo.Shop;
import com.zut.utils.DruidUtil;
import com.zut.vo.ShopVo;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 张晨
 * @date 2024/9/12 22:00
 * @desc
 */
public class AdminShopDaoImpl implements AdminShopDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(DruidUtil.getDataSource());
    @Override
    public List<ShopVo> selectAll(Shop shop, Integer currentPage, Integer pageSize) {
        String sql = "select * from shop,shop_category where shop.shop_category_id = shop_category.shop_category_id";
        List list = new ArrayList();
        if (shop.getStatus() != null) {
            sql+=" and shop.status = ? ";
            list.add(shop.getStatus());
        }
        if (!StringUtils.isEmpty(shop.getShopName())) {
            sql+=" and shop.shop_name like concat('%',?,'%') ";
            list.add(shop.getShopName());
        }

        if (list.size() > 0) {
            return jdbcTemplate.query(sql,new BeanPropertyRowMapper<ShopVo>(ShopVo.class),list.toArray());
        }else {
            return jdbcTemplate.query(sql,new BeanPropertyRowMapper<ShopVo>(ShopVo.class));
        }
    }

    @Override
    public Long selectAllCount(Shop shop) {

        String sql = "select count(*) from shop,shop_category where shop.shop_category_id = shop_category.shop_category_id";
        List list = new ArrayList();
        if (shop.getStatus() != null) {
            sql+=" and shop.status = ? ";
            list.add(shop.getStatus());
        }
        if (!StringUtils.isEmpty(shop.getShopName())) {
            sql+=" and shop.shop_name like concat('%',?,'%') ";
            list.add(shop.getShopName());
        }

        if (list.size() > 0) {
            return jdbcTemplate.queryForObject(sql, Long.class,list.toArray());
        }else {
            return jdbcTemplate.queryForObject(sql, Long.class);
        }
    }
}
